package rps;

import java.util.HashMap;
import client.*;
import common.*;
import game.Bolera;
import game.Modelo;
import game.asignarTurno;
import game.records;

import javax.swing.JOptionPane;


/**
 */
public class RPSClient extends GameClient {

    /** reference to ConsoleEventReader that reads events from user input */
    protected RPSConsoleEventReader consoleReader;
    

    /**
     * main.
     */
    public static RPSClient clienteConsola(String args[], HashMap mapa) {
    //args = new String [2];
    //args={"127.0.0.1","jhony"};
    //args[0]="192.168.0.5";
	//args[1]="angela";
    // setup log4j

	if (args.length < 2) {
	    System.out.println("usage: RPSClient <host> <NickName>\n");
	    //System.exit(0);
	    //System.out.println("se ingresara <192.168.0.5> <jhony>\n");
	   // args[]={"192.168.0.5",""};
	   // args[1]="jhony";
	}

	// fire up the client
	RPSClient gc = new RPSClient();
	try {
		System.out.println("ip: "+args[0]+" puerto: "+mapa.get("puerto"));
		if(gc.testConect(args[0],Integer.parseInt(mapa.get("puerto")+"")))
			System.out.print("Se conecto.");
		else 
		{
			JOptionPane.showMessageDialog(null, "Problema conectando con el servidor. Verifique la Configuracion.");
			return null;
		}
	} 
	catch (Exception e) 
	{
		JOptionPane.showMessageDialog(null, "Problema conectando con el servidor. Verifique la Configuracion.");
		System.out.println("Problema conectando con el servidor");	
		return null;
	}	
	args[2]=mapa.get("puerto")+"";
	gc.init(args);
	gc.start();
	return gc;
    }

    /** 
     * call GameClient.init() 
     * and start our ConsoleEventReader
     * args[0] --> ip
     * args[1] --> nombre
     * args[2] --> puerto
     */
    public void init(String args[]) {
	super.init(args);
	consoleReader = new RPSConsoleEventReader(this, inQueue, outQueue);
	consoleReader.start();
    }
	
    /** 
     * shutdown the client
     */
    protected void shutdown() {
	consoleReader.shutdown();
	super.shutdown();
    }

    /** 
     * Encargado de manejar eventos entrantes de el EventQueue
     */
    protected void processIncomingEvents() {
    	//log.info(" processIncomingEvents ");
	GameEvent inEvent;
	while (inQueue.size() > 0) {
	    try {
		inEvent = inQueue.deQueue();
		System.out.println(" processIncomingEvents --> nick "+inEvent.getNick()+" personaje "+inEvent.getImagenJugador());
		switch (inEvent.getType()) {
		case GameEventDefault.S_LOGIN_ACK_OK:
		    break;
		case GameEventDefault.SB_LOGIN:
		    stdOut( "login: " + inEvent.getMessage());
		    break;
		case GameEventDefault.SB_LOGOUT:
		    stdOut( "logout: " + inEvent.getMessage());
		    break;
		case GameEventDefault.SB_CHAT_MSG:
		    stdOut( inEvent.getPlayerId() + ": " + inEvent.getMessage());
		    break;
		case GameEventDefault.SB_JUGAR_GAME:
				HashMap mapa= new HashMap();
				mapa.put("nick", inEvent.getNick());
				mapa.put("personaje", inEvent.getImagenJugador());
				mapa.put("bloqueado", inEvent.getBloqueado());
				Bolera.iniciarPartida(mapa, this);
		    break;
		case GameEventDefault.SB_CAMBIO_TURNO:
			HashMap mapa1= new HashMap();
			mapa1.put("nick", inEvent.getNick());
			mapa1.put("personaje", inEvent.getImagenJugador());
			mapa1.put("bloqueado", inEvent.getBloqueado());
			Bolera.cambiarTurno(mapa1);
	    break;
		case GameEventDefault.S_JUGAR_GAME_ACK_OK:
				asignarTurno.asignaTurno(inEvent.getMessage(), inEvent.getImagenJugador(), inEvent.getNick(), this);
	    break;
	    case GameEventDefault.SB_LANZAR:
			  Modelo.lanzarBola(inEvent.getNX(), inEvent.getNAngulo(), inEvent.getNvelocidad(),
					  inEvent.getEstadoS(),inEvent.getDxS(),inEvent.getDrS());
			break;
			
	    case GameEventDefault.S_CONSULTA_RECORDS_ACK_OK:
	    	records.mostrarRecords(inEvent.getMessage());
			break;	
		case GameEventDefault.S_DISCONNECT: 
		    stdErr( "disconnected from server: " + inEvent.getMessage());
		    shutdown();
		    break;
		case GameEventDefault.S_JOIN_GAME_ACK_OK:
		    stdOut( inEvent.getMessage());
		    inGame = true;
		    break;
		case GameEventDefault.S_JOIN_GAME_ACK_FAIL:
		    stdOut( inEvent.getMessage());
		    inGame = false;
		    break;
		case GameEventDefault.SB_PLAYER_QUIT:
		    stdOut( inEvent.getMessage());
		    inGame = false;
		    break;		    
		default:
		    stdOut( inEvent.getMessage());
		    break;
		}
	    }
	    catch (InterruptedException ie) {}
	}
    }

    /** 
     * return our GameName
     */
    public String getGameName() {
	return "Bowling NET";
    }

    /** 
     * factory method to create GameEvents
     */
    public GameEvent createGameEvent() {
	return new GameEventDefault();
    }
    
    /** 
     * factory method to create login GameEvents
     */
    public GameEvent createLoginEvent() {
	return new GameEventDefault(GameEventDefault.C_LOGIN);
    }
    
    /** 
     * factory method to create disconnect GameEvents
     */
    public GameEvent createDisconnectEvent(String reason) {
	return new GameEventDefault(GameEventDefault.S_DISCONNECT, reason);
    }
    
}
